mini.parse();

function GoodsAccessoriesHandler() {
};

GoodsAccessoriesHandler.instance = new GoodsAccessoriesHandler();

//获得全局的可选配件数据表格对象
var selectGrid = mini.get("selectGrid");

//获得全局的已选配件数据表格对象
var selectedGrid = mini.get("selectedGrid");

// 加载所有配件
selectGrid.load();

// 加载已选配件
selectedGrid.load({bgiId: bgiId});

//返回商品列表
GoodsAccessoriesHandler.returnGoods = function () {
	window.location.href = "goodsList.jsp?bgiId="+bgiId;
};

/**
 * 刷新页面数据及布局
 */
GoodsAccessoriesHandler.afresh = function () {
	window.location.href="goodsAccessories.jsp?bgiId="+bgiId+"&bgiName="+bgiName;
//	mini.get("messageReplyPanel").doLayout();
};

/**
 * 初始化商品查询面板
 */
GoodsAccessoriesHandler.initQueryForm = function(){
	GoodsQueryHandler.getBgbCombobox();
	GoodsQueryHandler.getBgsCombobox();
	mini.get("queryGoodsCommend").setData(GoodsQueryHandler.goodsCommendTypeData);
	mini.get("queryGoodsCommend").setValue(GoodsQueryHandler.goodsCommendTypeData[0].id);
}

GoodsAccessoriesHandler.initQueryForm();

GoodsAccessoriesHandler.doQueryGoods = function () {
	var goodsQueryData = GoodsQueryHandler.goodsQueryForm.getData();
	var json = mini.encode(goodsQueryData);
   	selectGrid.load({goodsQueryData: json});
}

selectedGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;

    if(column.field == "operation"){
    	e.cellHtml = '<a class="New_Button" href="javascript:void(0);" onclick="removeGoodsAccess();">删除</a>';
    }
});

function add() {        
    var items = selectGrid.getSelecteds();
	doAddItems(items);
}

function addAll() {        
    var items = selectGrid.getData();
    doAddItems(items);
}

function removeGoodsAccess() {
    var items = selectedGrid.getSelecteds();
    selectedGrid.removeRows(items);
}    

function doAddItems(items) {
    items = mini.clone(items);
    
    //根据id判断，去除重复的item
    for (var i = items.length - 1; i >= 0; i-- ) {
        var item = items[i];
        var item2 = selectedGrid.findRow(function (row) {
            if (row.bgiName == item.bgiName) return true;
        });
        if (item2) {
            items.removeAt(i);
        }
    }
    selectedGrid.addRows(items);
}

function saveItems() {
	var data = selectedGrid.getData();
	data = mini.clone(data);
	var json = mini.encode(data);
	
	mini.confirm("确认提交更改？" ,"系统提示",
		function(action, value){
			if (action == "ok") {
				var ids = "";
				for ( var i = 0; i < data.length - 1; i++){
					ids += data[i].bgiId+ ",";
				}
				if(data.length!=0)
				ids += data[data.length - 1].bgiId;
                $.ajax({
                    url: __root + "/addAccessories.do",
                    data : {
		            	bgiIds : ids,
		            	bgiId : bgiId,
		            	lslUser: gbOperateUser,
		    			lslAddress: gbOperateIp
		            },
                    success: function (text) {
                    	selectedGrid.unmask();
                    	MiniuiTools.miniInfo("系统提示","操作成功！", function(){
        					selectedGrid.reload();
        				});
	                },
	                error: function (jqXHR, textStatus, errorThrown) {
	            			selectedGrid.unmask();
	        				MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
	                    }
	                });	
			} 
		}
	)
}